<?php

class Workset_View_Helper_JQueryButton extends ZendX_JQuery_View_Helper_UiWidget {

    function jQueryButton($id, $value = null, $jQueryParams = array(), $attribs = array()) {
        $attribs = $this->_prepareAttributes($id, $value, $attribs);

		if (isset($attribs['id'])) {
			$htmlId = $attribs['id'];
		} else {
			$htmlId = $id;
		}

        if (isset($jQueryParams['events'])) {
            $jsEvents = array(sprintf('%s("#%s")',
                ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
                $htmlId
            ));

            foreach ($jQueryParams['events'] as $key => $code) {
                $jsEvents[] = sprintf("%s(function() { %s })", $key, $code);
            }
            $jsEvents = implode('.', $jsEvents);
            $this->jquery->addOnLoad($jsEvents);

            unset($jQueryParams['events']);
        }

        if(count($jQueryParams) > 0) {
            $jQueryParams = ZendX_JQuery::encodeJson($jQueryParams);
		} else {
		    $jQueryParams = "{}";
		}

		//type of button
		$type = !empty($attribs['type']) ? $attribs['type'] : 'submit';

		//display text
		$text = isset($attribs['title']) ? $attribs['title']
										 : (null !== $value ? $value : 'Save');

		unset($attribs['type']);
		unset($attribs['title']);

		if (null !== $value) {
			$attribs['value'] = (string)$value;
		}

        $js = sprintf('%s("#%s").button(%s);',
		    ZendX_JQuery_View_Helper_JQuery::getJQueryHandler(),
		    $htmlId,
		    $jQueryParams
		);

		$this->jquery->addOnLoad($js);

		$xhtml = sprintf('<button id="%s" type="%s"%s>%s</button>',
			$htmlId,
			$type,
			$this->_htmlAttribs($attribs),
			$text
		);

        return $xhtml;
    }
    
}

?>